Page({
	data: {
		word: '',
		meaning: [],
		sentence: '',
		translation: '',
		isLoading: false,
		type: 0,
		pageTitle: '',
		array: [
			'n.',
			'pron.',
			'v.',
			'vi.',
			'vt.',
			'adj.',
			'adv.',
			'art.',
			'prep.',
			'conj.',
		],
		index: 0,
	},

	wordInputHandler(e) {
		this.setData({ word: e.detail.value.trim() })
	},

	sentenceInputHandler(e) {
		this.setData({ sentence: e.detail.value })
	},

	sentenceTranslationInputHandler(e) {
		this.setData({ translation: e.detail.value })
	},

	pickerChangeHandler(e) {
		const currentIndex = e.currentTarget.dataset.current
		const { meaning, array } = this.data
		meaning[currentIndex].tag = array[e.detail.value]
		this.setData({ meaning })
	},

	translationInputHandler(e) {
		const currentIndex = e.currentTarget.dataset.current
		const { meaning } = this.data
		meaning[currentIndex].translation = e.detail.value
		this.setData({ meaning })
	},

	longpressHandler(e) {
		const { meaning } = this.data
		const _this = this
		wx.showModal({
			title: 'Delete',
			content: 'Do you want to delete this one?',
			confirmText: 'Yes',
			cancelText: 'Cancel',
			success(action) {
				if (action.confirm) {
					meaning.splice(e.currentTarget.dataset.current, 1)
					_this.setData({ meaning })
				}
			},
		})
	},

	addMeaning() {
		const { meaning } = this.data
		const newMeaning = {
			tag: 'n.',
			translation: '',
		}
		meaning.push(newMeaning)
		this.setData({ meaning })
	},

	submitWordHandler() {
		// 不可以不输入单词
		if (!this.data.word.trim()) {
			wx.showToast({
				title: 'Empty Word',
				icon: 'error',
				duration: 1500,
			})
			return
		}
		// 不能没有意义
		if (!this.data.meaning.length) {
			wx.showToast({
				title: 'No Meaning',
				icon: 'error',
				duration: 1500,
			})
			return
		}
		// 不可以有空的意义
		if (!this.data.meaning.find(mean => mean.translation)) {
			wx.showToast({
				title: 'No Translation',
				icon: 'error',
				duration: 1500,
			})
			return
		}
		wx.showLoading({
			title: 'Submitting...',
      mask: true
		})
		this.setData({ isLoading: true })
		if (this.data.wordid) {
			wx.cloud
				.callFunction({
					name: 'updateWord',
					data: {
						word: this.data.word,
						meaning: this.data.meaning,
						id: this.data.wordid,
					},
				})
				.then(() => {
					this.setData({ isLoading: false })
					wx.hideLoading()
					wx.redirectTo({
						url: '../all_words/index',
					})
				})
		} else {
			wx.cloud
				.callFunction({
					name: 'addWord',
					data: {
						spell: this.data.word,
						meaning: this.data.meaning,
					},
				})
				.then(() => {
					this.setData({ isLoading: false })
					wx.hideLoading()
					wx.redirectTo({
						url: '../status/index?type=1',
					})
				})
		}
	},

	submitSentenceHandler() {
		if (!this.data.sentence.trim()) {
			wx.showToast({
				title: 'Empty Sentence',
				icon: 'error',
				duration: 1500,
			})
			return
		}
		if (!this.data.translation.trim()) {
			wx.showToast({
				title: 'Empty Translation',
				icon: 'error',
				duration: 1500,
			})
			return
		}
		wx.showLoading({
			title: 'Submmitting...',
		})
		wx.cloud
			.callFunction({
				name: 'addSentence',
				data: {
					content: this.data.sentence,
					translation: this.data.translation,
				},
			})
			.then(() => {
				wx.hideLoading()
				wx.redirectTo({
					url: '../status/index?type=2',
				})
			})
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function (options) {
		const title = options.type == 1 ? options.wordid ? 'Update Word' : 'Enter Word' : 'Enter Sentence'
		const word = options.word || ''
		const meaning = options.meaning ? JSON.parse(options.meaning) : []
		const wordid = options.wordid || ''
		this.setData({
			type: options.type,
			pageTitle: title,
			word,
			meaning,
			wordid,
			mode: wx.getStorageSync('vision-mode')
		})
	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function () {},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function () {},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function () {},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function () {},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function () {},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function () {},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function () {},
})
